/**
 * @author LKQ
 * @date 2022/1/20 16:31
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.multiply("123", "456");
    }
    public String multiply(String num1, String num2) {
        if (num1.equals("0") || num2.equals("0")) {
            return "0";
        }
        String ans = "0";
        int m = num1.length(), n = num2.length();
        for (int i = n-1; i >=0 ; i--) {
            StringBuffer cur = new StringBuffer();
            int add = 0;
            for (int j = n-1; j > i; j--) {
                cur.append(0);
            }
            int y = num2.charAt(i) - '0';
            for (int j = m - 1; j >=0; j--) {
                int x = num1.charAt(j) - '0';
                int product = x * y + add;
                cur.append(product % 10);
                add = product / 10;
            }
            if (add != 0) {
                cur.append(add % 10);
            }
            ans = addStrings(ans, cur.reverse().toString());
        }
        return ans;
    }

    public String addStrings(String num1, String num2) {
        StringBuffer sb = new StringBuffer();
        int m = num1.length(), n = num2.length();
        int p = 0, q = 0, add = 0;
        while (p < m || q < n || add != 0) {
            int number1 = p >= m ? 0 : num1.charAt(m - p - 1) - '0';
            int number2 = q >= n ? 0 : num2.charAt(n - q - 1) - '0';
            int result = number1 + number2 + add;
            sb.append(result % 10);
            add = result / 10 ;
            p++;
            q++;
        }
        return sb.reverse().toString();
    }
}
